<?php
namespace app\client\controller;

use app\client\lib\Alioss;
use Intervention\Image\ImageManagerStatic as Image;
use app\common\lib\Redis;

class Upload extends Base {
    
    private $file = null;
    
    public function __construct() {
        parent::__construct();
        
        $this->file = request()->file();
    }
    
    /**
     * 上传图片
     * **/
    public function uploadpic() {
        $file = $this->file['file'];
        $info = $file->getInfo();
        
        if(!$file->isValid()){ //判断是不是通过上传的文件
            return errorResult();
        }
        
        $imagesize = getimagesize($info['tmp_name']); //判断是不是真的图片文件
        if(!$imagesize[0] || !$imagesize[1]){
            return errorResult();
        }
        
        if(!$file->check(['size' => 10 * 1024 * 1024, 'ext' => ['png', 'jpg', 'jpeg']])){ //判断图片大小和类型
            return errorResult('图片最大10M');
        }
        
        
        $extension = pathinfo($info['name'])['extension'];
        
        $redis = new Redis();
        $ACCESS_TOKEN = $redis->get('wx_access_token');
        $redis->close();
        
        $cfile = curl_file_create($info['tmp_name'], 'image/' . $extension, 'media');
        $ch = curl_init('https://api.weixin.qq.com/wxa/img_sec_check?access_token=' . $ACCESS_TOKEN);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ['access_token' => $ACCESS_TOKEN, 'media' => $cfile]);
        $result = curl_exec($ch);
        curl_close($ch);
        if(empty($result)) {
            return errorResult();
        }
        $result = json_decode($result, true);
        if(empty($result)) {
            return errorResult();
        }
        if($result['errcode'] != 0) {
            return errorResult('图片含有违法违规内容');
        }
        
        
        $fileName = $this->buildSaveName($info['name']); //文件名
        
        $thumbPath = env('ROOT_PATH') . '/public/thumb/' . date('Ymd');

        if(!is_dir($thumbPath)){
            mkdir($thumbPath, 0777, true);
        }
        
        $thubmPath = $thumbPath . '/' . $fileName;
        
        $image = \think\Image::open($file);
        $image->thumb(200, 200, \think\Image::THUMB_CENTER, null, 100)->save($thubmPath); //保存缩略图

        
        $alioss = new Alioss();
        $bucketName = 'static-2019';
        if(!$alioss->doesBucketExist($bucketName)){
            $result = $alioss->createBucket($bucketName);
        }
        
        $result = $alioss->uploadFile($bucketName, $fileName, $info['tmp_name']); //保存大图到阿里云
        if(!$result){
            return errorResult('上传图片失败');
        }
        
        $result = $alioss->uploadFile($bucketName, "thumb/{$fileName}", $thubmPath); //保存缩略图到阿里云
        if(!$result){
            return errorResult('上传图片失败');
        }
        
        
        return successResult(['fileName' => $fileName]);
        
    }
    
    
    /**
     * 构造文件名
     * **/
    protected function buildSaveName($fileName){
        $ext = pathinfo($fileName, PATHINFO_EXTENSION);
        return md5($fileName . microtime(true)) . '.' . $ext;
    }
    
}










